package com.bookmanager.www.common.exception;



import com.bookmanager.www.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.AuthenticationException;


/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/08/31/23:36
 * @Description: 全局Valid 异常类  这个已经转入GlobalExceptionHandler 处理 暂时不启用
 */
@Slf4j
//@ControllerAdvice
public class VaildExceptionConfig {

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result throwCustomException(MethodArgumentNotValidException methodArgumentNotValidException) {

        log.info("[ @Vaild异常捕获 ] " + methodArgumentNotValidException.getMessage());

        return Result.fail("",methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public Result throwNullPointerException(NullPointerException nullPointerException) {

        log.info("[ 空指针异常捕获 ] " + nullPointerException.getMessage());

        return Result.fail("",nullPointerException.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(AuthenticationException.class)
    public Result throwAuthenticationException(AuthenticationException authenticationException) {

        log.info("[ 认证异常捕获 ] " + authenticationException.getMessage());

        return Result.fail("",authenticationException.getMessage());
    }

}